{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RTpJ8axD8j1Z",
        "outputId": "f52a81c7-de6b-46de-8894-0fd522e68942"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.1/70.1 KB\u001b[0m \u001b[31m2.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m18.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m158.8/158.8 KB\u001b[0m \u001b[31m11.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m199.2/199.2 KB\u001b[0m \u001b[31m10.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.2/114.2 KB\u001b[0m \u001b[31m2.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m264.6/264.6 KB\u001b[0m \u001b[31m10.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip -q install openai"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1wucl58v8Z4M",
        "outputId": "9374a594-4a68-4405-e322-33d28b7fb31f"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Cloning into 'stanford_alpaca'...\n",
            "remote: Enumerating objects: 94, done.\u001b[K\n",
            "remote: Counting objects: 100% (38/38), done.\u001b[K\n",
            "remote: Compressing objects: 100% (16/16), done.\u001b[K\n",
            "remote: Total 94 (delta 24), reused 23 (delta 22), pack-reused 56\u001b[K\n",
            "Unpacking objects: 100% (94/94), 9.13 MiB | 3.65 MiB/s, done.\n"
          ]
        }
      ],
      "source": [
        "!git clone https://github.com/tatsu-lab/stanford_alpaca.git"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "e7GbIimj8-15",
        "outputId": "4306d942-311c-4ad8-b453-b47d2913fdfc"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "/content/stanford_alpaca\n"
          ]
        }
      ],
      "source": [
        "%cd stanford_alpaca"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tibz9rzb9mKb"
      },
      "source": [
        "## Alpaca Data Creator\n",
        "\n",
        "\n",
        "<img src=\"https://github.com/tatsu-lab/stanford_alpaca/blob/main/assets/parse_analysis.png?raw=1\" alt=\"example image\" width=\"600\">"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fLiaceig9A8l",
        "outputId": "fcb6128c-bc2e-453b-a43f-61011fd9bf9d"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.9/dist-packages (from -r requirements.txt (line 1)) (1.22.4)\n",
            "Collecting rouge_score\n",
            "  Downloading rouge_score-0.1.2.tar.gz (17 kB)\n",
            "  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "Collecting fire\n",
            "  Downloading fire-0.5.0.tar.gz (88 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m88.3/88.3 KB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "Requirement already satisfied: openai in /usr/local/lib/python3.9/dist-packages (from -r requirements.txt (line 4)) (0.27.2)\n",
            "Collecting transformers>=4.26.1\n",
            "  Downloading transformers-4.27.1-py3-none-any.whl (6.7 MB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.7/6.7 MB\u001b[0m \u001b[31m68.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: torch in /usr/local/lib/python3.9/dist-packages (from -r requirements.txt (line 6)) (1.13.1+cu116)\n",
            "Collecting sentencepiece\n",
            "  Downloading sentencepiece-0.1.97-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m12.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting tokenizers==0.12.1\n",
            "  Downloading tokenizers-0.12.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (6.6 MB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.6/6.6 MB\u001b[0m \u001b[31m90.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting wandb\n",
            "  Downloading wandb-0.14.0-py3-none-any.whl (2.0 MB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m87.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: absl-py in /usr/local/lib/python3.9/dist-packages (from rouge_score->-r requirements.txt (line 2)) (1.4.0)\n",
            "Requirement already satisfied: nltk in /usr/local/lib/python3.9/dist-packages (from rouge_score->-r requirements.txt (line 2)) (3.7)\n",
            "Requirement already satisfied: six>=1.14.0 in /usr/local/lib/python3.9/dist-packages (from rouge_score->-r requirements.txt (line 2)) (1.15.0)\n",
            "Requirement already satisfied: termcolor in /usr/local/lib/python3.9/dist-packages (from fire->-r requirements.txt (line 3)) (2.2.0)\n",
            "Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.9/dist-packages (from openai->-r requirements.txt (line 4)) (2.25.1)\n",
            "Requirement already satisfied: aiohttp in /usr/local/lib/python3.9/dist-packages (from openai->-r requirements.txt (line 4)) (3.8.4)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.9/dist-packages (from openai->-r requirements.txt (line 4)) (4.65.0)\n",
            "Requirement already satisfied: filelock in /usr/local/lib/python3.9/dist-packages (from transformers>=4.26.1->-r requirements.txt (line 5)) (3.9.1)\n",
            "Collecting huggingface-hub<1.0,>=0.11.0\n",
            "  Downloading huggingface_hub-0.13.2-py3-none-any.whl (199 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m199.2/199.2 KB\u001b[0m \u001b[31m22.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.9/dist-packages (from transformers>=4.26.1->-r requirements.txt (line 5)) (6.0)\n",
            "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.9/dist-packages (from transformers>=4.26.1->-r requirements.txt (line 5)) (23.0)\n",
            "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.9/dist-packages (from transformers>=4.26.1->-r requirements.txt (line 5)) (2022.6.2)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.9/dist-packages (from torch->-r requirements.txt (line 6)) (4.5.0)\n",
            "Requirement already satisfied: protobuf!=4.21.0,<5,>=3.15.0 in /usr/local/lib/python3.9/dist-packages (from wandb->-r requirements.txt (line 9)) (3.19.6)\n",
            "Collecting pathtools\n",
            "  Downloading pathtools-0.1.2.tar.gz (11 kB)\n",
            "  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "Requirement already satisfied: psutil>=5.0.0 in /usr/local/lib/python3.9/dist-packages (from wandb->-r requirements.txt (line 9)) (5.4.8)\n",
            "Collecting sentry-sdk>=1.0.0\n",
            "  Downloading sentry_sdk-1.17.0-py2.py3-none-any.whl (189 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m189.1/189.1 KB\u001b[0m \u001b[31m19.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting GitPython!=3.1.29,>=1.0.0\n",
            "  Downloading GitPython-3.1.31-py3-none-any.whl (184 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m184.3/184.3 KB\u001b[0m \u001b[31m9.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: setuptools in /usr/local/lib/python3.9/dist-packages (from wandb->-r requirements.txt (line 9)) (63.4.3)\n",
            "Collecting appdirs>=1.4.3\n",
            "  Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)\n",
            "Requirement already satisfied: Click!=8.0.0,>=7.0 in /usr/local/lib/python3.9/dist-packages (from wandb->-r requirements.txt (line 9)) (8.1.3)\n",
            "Collecting docker-pycreds>=0.4.0\n",
            "  Downloading docker_pycreds-0.4.0-py2.py3-none-any.whl (9.0 kB)\n",
            "Collecting setproctitle\n",
            "  Downloading setproctitle-1.3.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30 kB)\n",
            "Collecting gitdb<5,>=4.0.1\n",
            "  Downloading gitdb-4.0.10-py3-none-any.whl (62 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.7/62.7 KB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: chardet<5,>=3.0.2 in /usr/local/lib/python3.9/dist-packages (from requests>=2.20->openai->-r requirements.txt (line 4)) (4.0.0)\n",
            "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests>=2.20->openai->-r requirements.txt (line 4)) (1.26.15)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests>=2.20->openai->-r requirements.txt (line 4)) (2.10)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests>=2.20->openai->-r requirements.txt (line 4)) (2022.12.7)\n",
            "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.9/dist-packages (from aiohttp->openai->-r requirements.txt (line 4)) (1.3.1)\n",
            "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /usr/local/lib/python3.9/dist-packages (from aiohttp->openai->-r requirements.txt (line 4)) (3.1.0)\n",
            "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.9/dist-packages (from aiohttp->openai->-r requirements.txt (line 4)) (1.8.2)\n",
            "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.9/dist-packages (from aiohttp->openai->-r requirements.txt (line 4)) (1.3.3)\n",
            "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.9/dist-packages (from aiohttp->openai->-r requirements.txt (line 4)) (6.0.4)\n",
            "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.9/dist-packages (from aiohttp->openai->-r requirements.txt (line 4)) (4.0.2)\n",
            "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.9/dist-packages (from aiohttp->openai->-r requirements.txt (line 4)) (22.2.0)\n",
            "Requirement already satisfied: joblib in /usr/local/lib/python3.9/dist-packages (from nltk->rouge_score->-r requirements.txt (line 2)) (1.1.1)\n",
            "Collecting smmap<6,>=3.0.1\n",
            "  Downloading smmap-5.0.0-py3-none-any.whl (24 kB)\n",
            "Building wheels for collected packages: rouge_score, fire, pathtools\n",
            "  Building wheel for rouge_score (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for rouge_score: filename=rouge_score-0.1.2-py3-none-any.whl size=24954 sha256=a68e56d251b2bcad90daa0c40daf87a64ba6f750802e1dd5dabbcb90f7b51e56\n",
            "  Stored in directory: /root/.cache/pip/wheels/9b/3d/39/09558097d3119ca0a4d462df68f22c6f3c1b345ac63a09b86e\n",
            "  Building wheel for fire (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for fire: filename=fire-0.5.0-py2.py3-none-any.whl size=116952 sha256=75396f4186809a7f220039504babb08560ff52bdc4ff957693da1b689dadd1e9\n",
            "  Stored in directory: /root/.cache/pip/wheels/f7/f1/89/b9ea2bf8f80ec027a88fef1d354b3816b4d3d29530988972f6\n",
            "  Building wheel for pathtools (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for pathtools: filename=pathtools-0.1.2-py3-none-any.whl size=8807 sha256=f95a272d7439935bd02d5d0696fe289f9c495a7670c5a38058ca968dcc816f7c\n",
            "  Stored in directory: /root/.cache/pip/wheels/b7/0a/67/ada2a22079218c75a88361c0782855cc72aebc4d18d0289d05\n",
            "Successfully built rouge_score fire pathtools\n",
            "Installing collected packages: tokenizers, sentencepiece, pathtools, appdirs, smmap, setproctitle, sentry-sdk, fire, docker-pycreds, rouge_score, huggingface-hub, gitdb, transformers, GitPython, wandb\n",
            "Successfully installed GitPython-3.1.31 appdirs-1.4.4 docker-pycreds-0.4.0 fire-0.5.0 gitdb-4.0.10 huggingface-hub-0.13.2 pathtools-0.1.2 rouge_score-0.1.2 sentencepiece-0.1.97 sentry-sdk-1.17.0 setproctitle-1.3.2 smmap-5.0.0 tokenizers-0.12.1 transformers-4.27.1 wandb-0.14.0\n"
          ]
        }
      ],
      "source": [
        "!pip install -r requirements.txt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "M5b0ALlsp8Eh"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import openai\n",
        "\n",
        "openai.api_key =''\n",
        "os.environ['OPENAI_API_KEY'] = ''"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SGbDrUbi_JJS"
      },
      "source": [
        "## Data Generations"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "id": "T2npPlCABnT2",
        "outputId": "15f70990-9f13-485b-9843-bf9a32eef380"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'\\nbatch_selfinstruct_generate.py\\nrun:\\npython -m generate_instruction generate_instruction_following_data   --output_dir ./   --num_instructions_to_generate 10   --model_name=\"text-davinci-003\" '"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "\"\"\"\n",
        "batch_selfinstruct_generate.py\n",
        "run:\n",
        "python -m generate_instruction generate_instruction_following_data \\\n",
        "  --output_dir ./ \\\n",
        "  --num_instructions_to_generate 10 \\\n",
        "  --model_name=\"text-davinci-003\" \\\n",
        "\"\"\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "qefoyuTtBjkn"
      },
      "outputs": [],
      "source": [
        "import time\n",
        "import json\n",
        "import os\n",
        "import random\n",
        "import re\n",
        "import string\n",
        "from functools import partial\n",
        "from multiprocessing import Pool\n",
        "\n",
        "import numpy as np\n",
        "import tqdm\n",
        "from rouge_score import rouge_scorer\n",
        "import utils\n",
        "\n",
        "import fire"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "JlvGPSJK_cT2"
      },
      "outputs": [],
      "source": [
        "\n",
        "def encode_prompt(prompt_instructions):\n",
        "    \"\"\"Encode multiple prompt instructions into a single string.\"\"\"\n",
        "    prompt = open(\"./prompt.txt\").read() + \"\\n\"\n",
        "\n",
        "    for idx, task_dict in enumerate(prompt_instructions):\n",
        "        (instruction, input, output) = task_dict[\"instruction\"], task_dict[\"input\"], task_dict[\"output\"]\n",
        "        instruction = re.sub(r\"\\s+\", \" \", instruction).strip().rstrip(\":\")\n",
        "        input = \"<noinput>\" if input.lower() == \"\" else input\n",
        "        prompt += f\"###\\n\"\n",
        "        prompt += f\"{idx + 1}. Instruction: {instruction}\\n\"\n",
        "        prompt += f\"{idx + 1}. Input:\\n{input}\\n\"\n",
        "        prompt += f\"{idx + 1}. Output:\\n{output}\\n\"\n",
        "    prompt += f\"###\\n\"\n",
        "    prompt += f\"{idx + 2}. Instruction:\"\n",
        "    return prompt\n",
        "\n",
        "\n",
        "def post_process_gpt3_response(num_prompt_instructions, response):\n",
        "    if response is None:\n",
        "        return []\n",
        "    raw_instructions = f\"{num_prompt_instructions+1}. Instruction:\" + response[\"text\"]\n",
        "    raw_instructions = re.split(\"###\", raw_instructions)\n",
        "    instructions = []\n",
        "    for idx, inst in enumerate(raw_instructions):\n",
        "        # if the decoding stops due to length, the last example is likely truncated so we discard it\n",
        "        if idx == len(raw_instructions) - 1 and response[\"finish_reason\"] == \"length\":\n",
        "            continue\n",
        "        idx += num_prompt_instructions + 1\n",
        "        splitted_data = re.split(f\"{idx}\\.\\s+(Instruction|Input|Output):\", inst)\n",
        "        if len(splitted_data) != 7:\n",
        "            continue\n",
        "        else:\n",
        "            inst = splitted_data[2].strip()\n",
        "            input = splitted_data[4].strip()\n",
        "            input = \"\" if input.lower() == \"<noinput>\" else input\n",
        "            output = splitted_data[6].strip()\n",
        "        # filter out too short or too long instructions\n",
        "        if len(inst.split()) <= 3 or len(inst.split()) > 150:\n",
        "            continue\n",
        "        # filter based on keywords that are not suitable for language models.\n",
        "        blacklist = [\n",
        "            \"image\",\n",
        "            \"images\",\n",
        "            \"graph\",\n",
        "            \"graphs\",\n",
        "            \"picture\",\n",
        "            \"pictures\",\n",
        "            \"file\",\n",
        "            \"files\",\n",
        "            \"map\",\n",
        "            \"maps\",\n",
        "            \"draw\",\n",
        "            \"plot\",\n",
        "            \"go to\",\n",
        "            \"video\",\n",
        "            \"audio\",\n",
        "            \"music\",\n",
        "            \"flowchart\",\n",
        "            \"diagram\",\n",
        "        ]\n",
        "        blacklist += []\n",
        "        if any(find_word_in_string(word, inst) for word in blacklist):\n",
        "            continue\n",
        "        # We found that the model tends to add \"write a program\" to some existing instructions, which lead to a lot of such instructions.\n",
        "        # And it's a bit comfusing whether the model need to write a program or directly output the result.\n",
        "        # Here we filter them out.\n",
        "        # Note this is not a comprehensive filtering for all programming instructions.\n",
        "        if inst.startswith(\"Write a program\"):\n",
        "            continue\n",
        "        # filter those starting with punctuation\n",
        "        if inst[0] in string.punctuation:\n",
        "            continue\n",
        "        # filter those starting with non-english character\n",
        "        if not inst[0].isascii():\n",
        "            continue\n",
        "        instructions.append({\"instruction\": inst, \"input\": input, \"output\": output})\n",
        "    return instructions\n",
        "\n",
        "\n",
        "def find_word_in_string(w, s):\n",
        "    return re.compile(r\"\\b({0})\\b\".format(w), flags=re.IGNORECASE).search(s)\n",
        "\n",
        "\n",
        "def generate_instruction_following_data(\n",
        "    output_dir=\"./\",\n",
        "    seed_tasks_path=\"./seed_tasks.jsonl\",\n",
        "    num_instructions_to_generate=100,\n",
        "    model_name=\"text-davinci-003\",\n",
        "    num_prompt_instructions=3,\n",
        "    request_batch_size=5,\n",
        "    temperature=1.0,\n",
        "    top_p=1.0,\n",
        "    num_cpus=16,\n",
        "):\n",
        "    seed_tasks = [json.loads(l) for l in open(seed_tasks_path, \"r\")]\n",
        "    seed_instruction_data = [\n",
        "        {\"instruction\": t[\"instruction\"], \"input\": t[\"instances\"][0][\"input\"], \"output\": t[\"instances\"][0][\"output\"]}\n",
        "        for t in seed_tasks\n",
        "    ]\n",
        "    print(f\"Loaded {len(seed_instruction_data)} human-written seed instructions\")\n",
        "\n",
        "    os.makedirs(output_dir, exist_ok=True)\n",
        "    request_idx = 0\n",
        "    # load the LM-generated instructions\n",
        "    machine_instruction_data = []\n",
        "    if os.path.exists(os.path.join(output_dir, \"regen.json\")):\n",
        "        machine_instruction_data = utils.jload(os.path.join(output_dir, \"regen.json\"))\n",
        "        print(f\"Loaded {len(machine_instruction_data)} machine-generated instructions\")\n",
        "\n",
        "    # similarities = {}\n",
        "    scorer = rouge_scorer.RougeScorer([\"rougeL\"], use_stemmer=False)\n",
        "\n",
        "    # now let's generate new instructions!\n",
        "    progress_bar = tqdm.tqdm(total=num_instructions_to_generate)\n",
        "    if machine_instruction_data:\n",
        "        progress_bar.update(len(machine_instruction_data))\n",
        "\n",
        "    # first we tokenize all the seed instructions and generated machine instructions\n",
        "    all_instructions = [d[\"instruction\"] for d in seed_instruction_data] + [\n",
        "        d[\"instruction\"] for d in machine_instruction_data\n",
        "    ]\n",
        "    all_instruction_tokens = [scorer._tokenizer.tokenize(inst) for inst in all_instructions]\n",
        "\n",
        "    while len(machine_instruction_data) < num_instructions_to_generate:\n",
        "        request_idx += 1\n",
        "\n",
        "        batch_inputs = []\n",
        "        for _ in range(request_batch_size):\n",
        "            # only sampling from the seed tasks\n",
        "            prompt_instructions = random.sample(seed_instruction_data, num_prompt_instructions)\n",
        "            prompt = encode_prompt(prompt_instructions)\n",
        "            batch_inputs.append(prompt)\n",
        "        decoding_args = utils.OpenAIDecodingArguments(\n",
        "            temperature=temperature,\n",
        "            n=1,\n",
        "            max_tokens=3072,  # hard-code to maximize the length. the requests will be automatically adjusted\n",
        "            top_p=top_p,\n",
        "            stop=[\"\\n20\", \"20.\", \"20.\"],\n",
        "        )\n",
        "        request_start = time.time()\n",
        "        results = utils.openai_completion(\n",
        "            prompts=batch_inputs,\n",
        "            model_name=model_name,\n",
        "            batch_size=request_batch_size,\n",
        "            decoding_args=decoding_args,\n",
        "            logit_bias={\"50256\": -100},  # prevent the <|endoftext|> token from being generated\n",
        "        )\n",
        "        request_duration = time.time() - request_start\n",
        "\n",
        "        process_start = time.time()\n",
        "        instruction_data = []\n",
        "        for result in results:\n",
        "            new_instructions = post_process_gpt3_response(num_prompt_instructions, result)\n",
        "            instruction_data += new_instructions\n",
        "\n",
        "        total = len(instruction_data)\n",
        "        keep = 0\n",
        "        for instruction_data_entry in instruction_data:\n",
        "            # computing similarity with the pre-tokenzied instructions\n",
        "            new_instruction_tokens = scorer._tokenizer.tokenize(instruction_data_entry[\"instruction\"])\n",
        "            with Pool(num_cpus) as p:\n",
        "                rouge_scores = p.map(\n",
        "                    partial(rouge_scorer._score_lcs, new_instruction_tokens),\n",
        "                    all_instruction_tokens,\n",
        "                )\n",
        "            rouge_scores = [score.fmeasure for score in rouge_scores]\n",
        "            most_similar_instructions = {\n",
        "                all_instructions[i]: rouge_scores[i] for i in np.argsort(rouge_scores)[-10:][::-1]\n",
        "            }\n",
        "            if max(rouge_scores) > 0.7:\n",
        "                continue\n",
        "            else:\n",
        "                keep += 1\n",
        "            instruction_data_entry[\"most_similar_instructions\"] = most_similar_instructions\n",
        "            instruction_data_entry[\"avg_similarity_score\"] = float(np.mean(rouge_scores))\n",
        "            machine_instruction_data.append(instruction_data_entry)\n",
        "            all_instructions.append(instruction_data_entry[\"instruction\"])\n",
        "            all_instruction_tokens.append(new_instruction_tokens)\n",
        "            progress_bar.update(1)\n",
        "        process_duration = time.time() - process_start\n",
        "        print(f\"Request {request_idx} took {request_duration:.2f}s, processing took {process_duration:.2f}s\")\n",
        "        print(f\"Generated {total} instructions, kept {keep} instructions\")\n",
        "        utils.jdump(machine_instruction_data, os.path.join(output_dir, \"regen.json\"))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eDzyIc89ENYR"
      },
      "source": [
        "## the data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LRiGYpxcHgvC",
        "outputId": "4be43f96-fba9-4237-d125-5c03ccc9a66b"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'instruction': 'What is the scientific name for a beaver?',\n",
              " 'input': '',\n",
              " 'output': 'The scientific name for a beaver is Castor canadensis.'}"
            ]
          },
          "execution_count": 55,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import json\n",
        "\n",
        "file_path = \"/content/stanford_alpaca/alpaca_data.json\"\n",
        "\n",
        "with open(file_path, 'r') as json_file:\n",
        "    data = json.load(json_file)\n",
        "\n",
        "# print(data)\n",
        "data[245]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_ztLh8ck0NAc"
      },
      "source": [
        "## Seed Tasks"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "co3sx8Ed0MxQ",
        "outputId": "6a487435-ee9a-4f83-a0e8-e73e64106547"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Loaded 175 human-written seed instructions\n"
          ]
        }
      ],
      "source": [
        "seed_tasks_path=\"./seed_tasks.jsonl\"\n",
        "\n",
        "seed_tasks = [json.loads(l) for l in open(seed_tasks_path, \"r\")]\n",
        "\n",
        "seed_instruction_data = [\n",
        "    {\"instruction\": t[\"instruction\"], \"input\": t[\"instances\"][0][\"input\"], \"output\": t[\"instances\"][0][\"output\"]}\n",
        "    for t in seed_tasks\n",
        "]\n",
        "print(f\"Loaded {len(seed_instruction_data)} human-written seed instructions\")\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IjIydavr0OQH",
        "outputId": "86a7a964-8c6e-45d2-aa7b-ab458c08d878"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'id': 'seed_task_12',\n",
              " 'name': 'explain_behavior',\n",
              " 'instruction': \"Explain human's behavior.\",\n",
              " 'instances': [{'input': 'Behavior: cry.',\n",
              "   'output': 'There could be many reasons why a person might cry. They could be feeling sad, scared, angry, or frustrated. Sometimes people cry when they are happy or relieved. There is no one answer to why people behave the way they do.'}],\n",
              " 'is_classification': False}"
            ]
          },
          "execution_count": 58,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "seed_tasks[12]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LABR8_l-2mJN"
      },
      "source": [
        "## Creating a new set of seed tasks"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7wBPgTpb0e40"
      },
      "outputs": [],
      "source": [
        "new_seed_tasks = seed_tasks[:3]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9F6BoaMm2krm",
        "outputId": "be9a9e2f-1a9a-4e55-a4f5-aca4a1189cda"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[{'id': 'seed_task_0',\n",
              "  'name': 'breakfast_suggestion',\n",
              "  'instruction': \"Is there anything I can eat for a breakfast that doesn't include eggs, yet includes protein, and has roughly 700-1000 calories?\",\n",
              "  'instances': [{'input': '',\n",
              "    'output': 'Yes, you can have 1 oatmeal banana protein shake and 4 strips of bacon. The oatmeal banana protein shake may contain 1/2 cup oatmeal, 60 grams whey protein powder, 1/2 medium banana, 1tbsp flaxseed oil and 1/2 cup watter, totalling about 550 calories. The 4 strips of bacon contains about 200 calories.'}],\n",
              "  'is_classification': False},\n",
              " {'id': 'seed_task_1',\n",
              "  'name': 'antonym_relation',\n",
              "  'instruction': 'What is the relation between the given pairs?',\n",
              "  'instances': [{'input': 'Night : Day :: Right : Left',\n",
              "    'output': 'The relation between the given pairs is that they are opposites.'}],\n",
              "  'is_classification': False},\n",
              " {'id': 'seed_task_2',\n",
              "  'name': 'one_sentence_description',\n",
              "  'instruction': 'Generate a one-sentence description for each of the following people.',\n",
              "  'instances': [{'input': '- Brack Obama\\n- Elon Musk\\n- Taylor Swift',\n",
              "    'output': '- Barack Hussein Obama II is an American politician who served as the 44th president of the United States from 2009 to 2017.\\n- Elon Musk is the founder, CEO, and chief engineer of SpaceX; angel investor, CEO and product architect of Tesla, Inc.; founder of The Boring Company; co-founder of Neuralink and OpenAI; president of the Musk Foundation; and owner and CEO of Twitter, Inc.\\n- Taylor Alison Swift is an American singer-songwriter.'}],\n",
              "  'is_classification': False}]"
            ]
          },
          "execution_count": 29,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "new_seed_tasks"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mtQxteMI21LS"
      },
      "source": [
        "{  \n",
        "    'id': 'seed_task_1',  \n",
        "  'name': 'antonym_relation',  \n",
        "  'instruction': 'What is the relation between the given pairs?',  \n",
        "  'instances': [{'input': 'Night : Day :: Right : Left',  \n",
        "    'output': 'The relation between the given pairs is that they are opposites.'}],  \n",
        "  'is_classification': False}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "MiiKwelz6GCz"
      },
      "outputs": [],
      "source": [
        "json_new_tasks = '''\n",
        "[\n",
        "    {\n",
        "        \"id\": \"new_seed_task_3\",\n",
        "        \"name\": \"vegetarian_dinner_options\",\n",
        "        \"instruction\": \"What are some vegetarian dinner options with high protein content?\",\n",
        "        \"instances\": [{\"input\": \"\",\n",
        "                      \"output\": \"Some vegetarian dinner options with high protein content include lentil soup, chickpea curry, tofu stir-fry, tempeh tacos, and quinoa stuffed peppers.\"}],\n",
        "        \"is_classification\": false\n",
        "    },\n",
        "    {\n",
        "        \"id\": \"new_seed_task_4\",\n",
        "        \"name\": \"refund_policy_explanation\",\n",
        "        \"instruction\": \"Can you explain your refund policy?\",\n",
        "        \"instances\": [{\"input\": \"\",\n",
        "                      \"output\": \"Our refund policy allows customers to request a refund within 30 days of purchase if they are unsatisfied with the product or service. To be eligible for a refund, the product must be in its original condition and packaging, and services must not have been completed.\"}],\n",
        "        \"is_classification\": false\n",
        "    },\n",
        "    {\n",
        "        \"id\": \"new_seed_task_5\",\n",
        "        \"name\": \"troubleshoot_wifi_connection\",\n",
        "        \"instruction\": \"I'm having trouble connecting to my Wi-Fi network. What should I do?\",\n",
        "        \"instances\": [{\"input\": \"\",\n",
        "                      \"output\": \"First, check if your device is within range of your Wi-Fi router. Then, verify that you have entered the correct network name and password. If the issue persists, try restarting both your device and the router. If you still cannot connect, contact your internet service provider for further assistance.\"}],\n",
        "        \"is_classification\": false\n",
        "    },\n",
        "    {\n",
        "        \"id\": \"new_seed_task_6\",\n",
        "        \"name\": \"change_password_instructions\",\n",
        "        \"instruction\": \"How do I change my password?\",\n",
        "        \"instances\": [{\"input\": \"\",\n",
        "                      \"output\": \"To change your password, log in to your account, go to the settings or account management section, and find the option to change your password. Enter your current password, then enter your new password twice for confirmation. Save your changes to update your password.\"}],\n",
        "        \"is_classification\": false\n",
        "    },\n",
        "    {\n",
        "        \"id\": \"new_seed_task_7\",\n",
        "        \"name\": \"lost_package_inquiry\",\n",
        "        \"instruction\": \"My package hasn't arrived yet. What should I do?\",\n",
        "        \"instances\": [{\"input\": \"\",\n",
        "                      \"output\": \"First, check your order confirmation email or account for the tracking number and delivery date estimate. If the delivery date has passed, contact the shipping carrier with your tracking number for an update. If the carrier cannot resolve the issue, please contact our customer support team for assistance.\"}],\n",
        "        \"is_classification\": false\n",
        "    },\n",
        "    {\n",
        "        \"id\": \"new_seed_task_8\",\n",
        "        \"name\": \"modify_order\",\n",
        "        \"instruction\": \"I need to modify my order. How can I do that?\",\n",
        "        \"instances\": [{\"input\": \"\",\n",
        "                      \"output\": \"To modify your order, please contact our customer support team as soon as possible with your order number and the changes you would like to make. Please note that modifications may not be possible if the order has already been processed or shipped.\"}],\n",
        "        \"is_classification\": false\n",
        "    }\n",
        "]\n",
        "'''\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MysTLv3y-gLa",
        "outputId": "44bb4c44-4c9d-44dc-8ae7-ea7795f6cd3a"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[{'id': 'new_seed_task_3', 'name': 'vegetarian_dinner_options', 'instruction': 'What are some vegetarian dinner options with high protein content?', 'instances': [{'input': '', 'output': 'Some vegetarian dinner options with high protein content include lentil soup, chickpea curry, tofu stir-fry, tempeh tacos, and quinoa stuffed peppers.'}], 'is_classification': False}, {'id': 'new_seed_task_4', 'name': 'refund_policy_explanation', 'instruction': 'Can you explain your refund policy?', 'instances': [{'input': '', 'output': 'Our refund policy allows customers to request a refund within 30 days of purchase if they are unsatisfied with the product or service. To be eligible for a refund, the product must be in its original condition and packaging, and services must not have been completed.'}], 'is_classification': False}, {'id': 'new_seed_task_5', 'name': 'troubleshoot_wifi_connection', 'instruction': \"I'm having trouble connecting to my Wi-Fi network. What should I do?\", 'instances': [{'input': '', 'output': 'First, check if your device is within range of your Wi-Fi router. Then, verify that you have entered the correct network name and password. If the issue persists, try restarting both your device and the router. If you still cannot connect, contact your internet service provider for further assistance.'}], 'is_classification': False}, {'id': 'new_seed_task_6', 'name': 'change_password_instructions', 'instruction': 'How do I change my password?', 'instances': [{'input': '', 'output': 'To change your password, log in to your account, go to the settings or account management section, and find the option to change your password. Enter your current password, then enter your new password twice for confirmation. Save your changes to update your password.'}], 'is_classification': False}, {'id': 'new_seed_task_7', 'name': 'lost_package_inquiry', 'instruction': \"My package hasn't arrived yet. What should I do?\", 'instances': [{'input': '', 'output': 'First, check your order confirmation email or account for the tracking number and delivery date estimate. If the delivery date has passed, contact the shipping carrier with your tracking number for an update. If the carrier cannot resolve the issue, please contact our customer support team for assistance.'}], 'is_classification': False}, {'id': 'new_seed_task_8', 'name': 'modify_order', 'instruction': 'I need to modify my order. How can I do that?', 'instances': [{'input': '', 'output': 'To modify your order, please contact our customer support team as soon as possible with your order number and the changes you would like to make. Please note that modifications may not be possible if the order has already been processed or shipped.'}], 'is_classification': False}]\n"
          ]
        }
      ],
      "source": [
        "new_seed_tasks = json.loads(json_new_tasks)\n",
        "print(new_seed_tasks)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GMs7BWWa-v1D"
      },
      "outputs": [],
      "source": [
        "with open('new_seed_tasks.jsonl', 'w') as outfile:\n",
        "    for task_dict in new_seed_tasks:\n",
        "        json.dump(task_dict, outfile)\n",
        "        outfile.write('\\n')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7EslAAQqBGYf"
      },
      "outputs": [],
      "source": [
        "!mkdir new_tasks"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "22co1vF0Nb7H",
        "outputId": "52f33e27-4f80-4b48-c1c4-e3348c65fb2f"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Loaded 6 human-written seed instructions\n",
            "  0% 0/5 [00:00<?, ?it/s]\n",
            "prompt_batches:   0% 0/1 [00:00<?, ?it/s]\u001b[A\n",
            "prompt_batches: 100% 1/1 [01:21<00:00, 81.67s/it]\n",
            "28it [01:22,  2.57it/s]Request 1 took 81.67s, processing took 1.32s\n",
            "Generated 29 instructions, kept 29 instructions\n",
            "29it [01:22,  2.86s/it]\n"
          ]
        }
      ],
      "source": [
        "!python -m generate_instruction generate_instruction_following_data --output_dir ./new_tasks/ --seed_tasks_path ./new_seed_tasks.jsonl --num_instructions_to_generate 5 --num_prompt_instructions 3 --request_batch_size 2 --num_cpus 4"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PitLnTF6Aznv",
        "outputId": "fe0f93e0-53e2-41e1-e8f6-3f7164389bfe"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Loaded 6 human-written seed instructions\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "  0%|          | 0/5 [00:00<?, ?it/s]\n",
            "prompt_batches:   0%|          | 0/1 [00:00<?, ?it/s]\u001b[A\n",
            "prompt_batches: 100%|██████████| 1/1 [01:06<00:00, 66.40s/it]\n",
            "29it [01:09,  2.41s/it]"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Request 1 took 66.40s, processing took 3.55s\n",
            "Generated 32 instructions, kept 29 instructions\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\n"
          ]
        }
      ],
      "source": [
        "generate_instruction_following_data(\n",
        "    output_dir=\"./new_tasks/\",\n",
        "    seed_tasks_path=\"./new_seed_tasks.jsonl\",\n",
        "    num_instructions_to_generate=5,\n",
        "    model_name=\"text-davinci-003\",\n",
        "    num_prompt_instructions=3,\n",
        "    request_batch_size=2,\n",
        "    temperature=1.0,\n",
        "    top_p=1.0,\n",
        "    num_cpus=4,\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ps-91LWXBXt5",
        "outputId": "25804749-e1b2-4f18-bec1-12a6c63693b0"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[{'instruction': 'How do I add a friend to my account?', 'input': '', 'output': 'To add a friend to your account, go to the Friends page and type their username or email address in the search bar. Select their profile when it appears and select the “Add Friend” button to send them a friend request. They will need to accept your request to be added to your friend list.'}, {'instruction': 'Could you summarize the article \"The power of storytelling”?', 'input': 'The article “The Power of Storytelling” by John Devon, published in the Harvard Business Review (HBR), talks about the power of storytelling to mobilize people, drive change, and create a shared understanding.', 'output': 'Devon argues that storytelling is a powerful tool for motivating people and creating a unified understanding. Through storytelling, leaders can inspire their employees and move people to action. He offers practical advice on how to craft stories that resonate and ignite enthusiasm and motivation among the team. He emphasizes the importance of connecting the stories to values, staying relevant to the audience, and tailoring the storytelling depending on the context.'}, {'instruction': 'Convert 42 Fahrenheit to Celsius', 'input': '', 'output': '42 degrees Fahrenheit is equivalent to 5.56 degrees Celsius.'}, {'instruction': 'How do I create an account?', 'input': '', 'output': 'To create an account, visit our website and select the \"Sign Up\" option. Enter your name, email address, and a password of your choice, then click \"Create Account\" to submit the form. You will then be taken to your account page and can begin using our services.'}, {'instruction': 'Could you recommend a book about leadership?', 'input': '', 'output': 'A great book about leadership is \"The 7 Habits of Highly Effective People\" by Stephen Covey. This book talks about how to become an effective leader and create better relationships with others. It explores the importance of having good habits and creating a balanced, meaningful life.'}, {'instruction': 'Can you explain the concept of machine learning?', 'input': '', 'output': 'Machine learning is the process of building models that can learn from data and make predictions or decisions without being explicitly programmed. It involves algorithms that detect patterns in data, enabling the computer to “learn” without needing to be explicitly programmed for all scenarios. This allows the models to become more accurate as they are fed more data.'}, {'instruction': 'What steps do I need to take to cancel my subscription?', 'input': '', 'output': 'To cancel your subscription, go to the subscription page and select the “Cancel Subscription” option. Follow the instructions on the page to confirm your cancellation and confirm that you have been billed for the last monthly period. Once canceled, you will no longer be able to access your subscription services.'}, {'instruction': 'What is the best way to optimize my website for SEO?', 'input': '', 'output': \"The best way to optimize your website for SEO is to create content that is relevant and useful to your target audience. Make sure to include relevant keywords in your content, create links between pages, and use meta tags to describe the content on each page. Additionally, you can use tools like Google Search Console to analyze and refine your website's performance.\"}, {'instruction': 'How can I improve my credit score?', 'input': '', 'output': 'To improve your credit score, make sure to always pay your bills on time, reduce your credit utilization, and check your credit report for any errors. You can also use a balance transfer to pay off high-interest debt, and sign up for an automated payment plan to ensure that all bills are paid in a timely manner.'}, {'instruction': 'Can you provide a list of cheap meals for dinner?', 'input': '', 'output': 'Here is a list of cheap dinner ideas: Spaghetti and Meatballs, Sloppy Joes, Grilled Cheese and Tomato Soup, Tacos and Nachos, Fried Rice, Mac and Cheese, Baked Potato and Broccoli, Lentil Curry, Egg Salad, and Stuffed Peppers.'}, {'instruction': 'Suggest three exercises I can do at home', 'input': '', 'output': 'Here are three exercises you can do at home: jumping jacks, push-ups, and squats. These exercises are effective at building strength and can be done in a short amount of time.'}, {'instruction': 'How do I post a comment on my blog?', 'input': '', 'output': 'To post a comment on your blog, sign in to your account and navigate to the blog post you would like to comment on. Select the “Add Comment” option and type your comment into the text box. Select “Post Comment” to submit the comment.'}, {'instruction': 'How do I contact customer support?', 'input': '', 'output': 'You can contact our customer support team by visiting our website and selecting the “Contact Us” option. Here, you will be able to fill out a support form, chat with us directly, or contact us by phone or email.'}, {'instruction': 'What is the gravitational force between two objects?', 'input': 'The two objects have masses of 2 kg and 4 kg respectively.', 'output': 'The gravitational force between the two objects is 8 N, based on the formula F = G*m1*m2/r^2, where G is the gravitational constant, m1 and m2 are the masses of the two objects, and r is the distance between the two objects.'}, {'instruction': 'Is there a limit to the amount of data that can be stored in a computer?', 'input': '', 'output': 'Yes, there is a limit to the amount of data that can be stored in a computer. The exact amount of data a computer can store depends on its memory size, which is typically measured in gigabytes (GB). Most personal computers can store up to 1 terabyte (TB) of data, which is equivalent to 1,024 GB.'}, {'instruction': 'What is the best way to clean a computer keyboard?', 'input': '', 'output': 'The best way to clean a computer keyboard is to turn it off and use a cloth dampened with water and soap to gently wipe the keycaps and surface. Do not use any solvents or abrasives, as this may damage the keys. After cleaning, allow the keyboard to dry completely before using it again.'}, {'instruction': 'I want to compare two products. What should I consider?', 'input': '', 'output': 'When comparing two products, consider factors such as price, features, performance, size, aesthetics, gain or discounts and warranties. Also look at customer reviews to help get an unbiased opinion of the product.'}, {'instruction': 'I want to find a hotel near my preferred destination. What should I do?', 'input': 'My preferred destination is London, UK', 'output': 'Start by researching online for hotels in London. Check price, amenities, and customer reviews for each hotel. You can also use online travel websites, such as TripAdvisor, to compare different hotels in London. Once you find a hotel you are interested in, contact the hotel directly to book a reservation.'}, {'instruction': 'What types of documents should I include in my resume?', 'input': '', 'output': 'Your resume should include a cover letter, professional summary, list of relevant qualifications, employment history, education, additional skills and certifications, and references. The documents should be tailored to the job you are applying for and should contain relevant keywords and phrases.'}, {'instruction': \"I'm having trouble with my printer. How can I fix it?\", 'input': '', 'output': \"First, check the connections between the printer and your computer to ensure they are secure. Then, try restarting your printer and resetting the printer's configuration settings. If the issue persists, contact the product manufacturer or customer support for assistance.\"}, {'instruction': 'What project management tools should I consider for my business?', 'input': '', 'output': 'There are many project management tools available that can be used for businesses of all sizes. Popular tools include Microsoft Project, Trello, Asana, Slack, Jira, Basecamp, and Wrike. Consider researching each tool to determine which one would be the best fit for your business needs.'}, {'instruction': \"How can I make sure I'm getting the best deals online?\", 'input': '', 'output': 'To ensure you are getting the best deals online, create a price comparison list of the items you are interested in. Additionally, use a price tracking tool, such as CamelCamelCamel, to compare prices over time. Finally, look for coupon codes and discounts offered by the retailer.'}, {'instruction': 'I want to start a blog. What should I do?', 'input': '', 'output': 'To start a blog, begin by selecting a platform that meets your blogging needs. Popular blogging platforms include WordPress, Blogger, and Tumblr. Once you have chosen a platform, decide on a unique domain name and hosting service. Finally, create content for your blog by writing posts and adding pictures, videos, and other relevant content.'}, {'instruction': 'My computer is running slowly. How can I speed it up?', 'input': '', 'output': 'First, try restarting your computer to see if it improves performance. Next, delete any unnecessary files and uninstall any programs you do not use. You can also can try disabling start-up programs that are not needed. Additionally, check your antivirus and firewall settings to ensure they are not causing the slowdown. Finally, updating your operating system may result in improved performance.'}, {'instruction': 'How do I write a cover letter?', 'input': '', 'output': 'When writing a cover letter, it is important to make sure you include a salutation, a brief introduction, a few paragraphs describing your qualifications, a conclusion, and a thank you. Additionally, make sure to tailor the letter to the job you are applying for and include relevant skills, experiences, and keywords.'}, {'instruction': 'What is SEO and how can I optimize my website?', 'input': '', 'output': \"Search engine optimization (SEO) is the process of increasing the visibility of a website in a search engine's unpaid results. To optimize your website for SEO, make sure your content is relevant, that you use keywords throughout your webpage, that your webpage is fast-loading, and that you use structuring tags such as titles and headings. Additionally, make sure all images are optimized, that you use backlinks, and that your website is mobile-friendly.\"}, {'instruction': 'I received an error message on my computer. What should I do?', 'input': '', 'output': 'When you receive an error message, write down the exact message and look it up online. If the error message is not specific, try restarting your computer. Additionally, check to make sure you have the latest updates and check the system requirements of the software that caused the error. If the issue persists, contact your system manufacturer or customer support for further assistance.'}, {'instruction': 'I want to make a presentation. How should I organize it?', 'input': '', 'output': 'When organizing a presentation, start by outlining the main points you want to discuss. Then, create an introduction and conclusion for your presentation. Finally, add slides for each main point, breaking them down into subpoints if needed. Additionally, consider adding visuals and making the slides look aesthetically pleasing.'}, {'instruction': 'I have some old photos I want to scan. What is the best way to do that?', 'input': '', 'output': 'The best way to scan old photos is to use a flatbed scanner. First, make sure the photos are in a condition to be scanned without causing any damage. Then, set the scanner to the desired resolution and place the photos on the scanner bed. Finally, scan each photo, making sure to save or rename each scanned photo as you go.'}]\n"
          ]
        }
      ],
      "source": [
        "import json\n",
        "\n",
        "file_path = \"/content/stanford_alpaca/new_tasks/regen.json\"\n",
        "\n",
        "with open(file_path, 'r') as json_file:\n",
        "    data = json.load(json_file)\n",
        "\n",
        "# drop the most similar as measured by Rouge\n",
        "for dictionary in data:\n",
        "    dictionary.pop('most_similar_instructions', None)\n",
        "    dictionary.pop('avg_similarity_score',None)\n",
        "\n",
        "\n",
        "print(data)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "W65CWwCoC4WS",
        "outputId": "97cbd412-2b24-482c-fd62-89ed58cb6274"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "29"
            ]
          },
          "execution_count": 60,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "len(data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ic8dJRNkDCsw",
        "outputId": "c1b4f817-3efb-4904-a02e-54bf5fb3a234"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'instruction': 'I want to compare two products. What should I consider?',\n",
              " 'input': '',\n",
              " 'output': 'When comparing two products, consider factors such as price, features, performance, size, aesthetics, gain or discounts and warranties. Also look at customer reviews to help get an unbiased opinion of the product.'}"
            ]
          },
          "execution_count": 61,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "data[16]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WVTDZcWFRRm0",
        "outputId": "f57352f7-fbb5-4a69-a3db-16b44ed8e66e"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'instruction': 'How do I create an account?',\n",
              " 'input': '',\n",
              " 'output': 'To create an account, visit our website and select the \"Sign Up\" option. Enter your name, email address, and a password of your choice, then click \"Create Account\" to submit the form. You will then be taken to your account page and can begin using our services.'}"
            ]
          },
          "execution_count": 51,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "data[3]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "nBznMWFzRePn",
        "outputId": "9da603cc-8813-4577-ea29-2be844e5ab21"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'instruction': 'Can you explain the concept of machine learning?',\n",
              " 'input': '',\n",
              " 'output': 'Machine learning is the process of building models that can learn from data and make predictions or decisions without being explicitly programmed. It involves algorithms that detect patterns in data, enabling the computer to “learn” without needing to be explicitly programmed for all scenarios. This allows the models to become more accurate as they are fed more data.'}"
            ]
          },
          "execution_count": 66,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "data[5]"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
